package cn.harvest.storage.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DAO {
	private Session session = null;
	private Query query = null;
	private Transaction transaction = null;
	
	//======================================
	// 单子模式
	private DAO() {

	}

	public static DAO getDAO() {
		DAO dao = new DAO();
		return dao;
	}
	//======================================

	public boolean create(Object obj) {

		boolean is = false;

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			session.save(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	public String createRid(Object obj) {

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();
		Serializable ser = null;
		try {
			ser = session.save(obj);
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return ser.toString();
	}

	public boolean delete(Object obj) {

		boolean is = false;

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			session.delete(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	public boolean update(Object obj) {

		boolean is = false;

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			session.update(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	public boolean deleteBySQL(String sql) {

		boolean is = false;

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			session.connection().createStatement().executeUpdate(sql);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	public List selectByHQL(String hql) {

		List list = null;

		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			query = session.createQuery(hql);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return list;
	}

	public List selectBySQL(String sql) {

		List list = null;
		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			query = session.createSQLQuery(sql);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return list;
	}

	public int updateBySQL(String sql) {

		int temp = 0;
		session = HibernateSessionFactory.getSession();
		transaction = session.beginTransaction();

		try {
			Connection conn = session.connection();
			PreparedStatement stmt = conn.prepareStatement(sql);
			temp = stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return temp;
	}
}
